﻿using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.AvailableStockServices.Dto;
using SmartWMS.Server.StatementServer.InventoryCapacityServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Z.EntityFramework.Plus;

namespace SmartWMS.Server.StatementServer.InventoryCapacityServices
{
    /// <summary>
    /// 库存容量服务
    /// </summary>
    public class InventoryCapacityServices : IInventoryCapacityServices
    {
        //库位表
        private readonly IRepository<Location> _location;
        //仓库表
        private readonly IRepository<Warehouse> _warehouse;
        //产品表
        private readonly IRepository<Product> _product;

        public InventoryCapacityServices(IRepository<Location> location, IRepository<Warehouse> warehouse, IRepository<Product> product)
        {
            _location = location;
            _warehouse = warehouse;
            _product = product;
        }

        /// <summary>
        /// 库存容量列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<InventoryCapacityDto>>> GetInventoryCapacityList(InventoryCapacitySelectDto input)
        {
            //库位表
            var LocationList = await _location.GetAllListAsync();
            //仓库表
            var WarehouseList = await _warehouse.GetAllListAsync();
            //产品表
            var ProductList = await _product.GetAllListAsync();

            var list = (from a in LocationList
                        join b in WarehouseList on a.WarehouseId equals b.Id
                        join c in ProductList on a.Id equals c.Id
                        select new InventoryCapacityDto
                        {
                            Name = b.Name,
                            LocationName = a.LocationName,
                            Inventory = c.Inventory,
                            LocationType = a.LocationType,
                            LocationCode = a.LocationCode,

                        }).ToList();


            if (input.Name!=null)
            {
                list = list.Where(x => x.Name == input.Name).ToList();
            }

            //总数量
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / input.pageSize);

            list = list.Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToList();

            return new Response<List<InventoryCapacityDto>>()
            {
                Data = list,
                TotalCount = totalCount,
                PageCount = pageCount
            };
        }


        /// <summary>
        /// 加载仓库
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Warehouse>>> BindWarehouseAsync()
        {
            var result = _warehouse.GetAllListAsync();
            return Response<List<Warehouse>>.Ok(await result);
        }
    }
}
